#include "mythread.h"

#include <QDebug>
#include <QThread>

Task1::Task1(QObject *parent) : QObject(parent)
{

}


void Task1::working(int n)
{
    qDebug() << QThread::currentThreadId() ;

    for(int i = 0; i < n; i++)
    {
        _v.push_back(qrand() % 100000);
    }


    emit sendArray(_v);
}




// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------


Task2::Task2(QObject *parent) : QObject(parent)
{

}


void Task2::working(QVector<int> v)
{
    int n = v.size();
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n - i - 1; j++)
        {
            if(v[j] > v[j + 1])
            {
                qSwap(v[j], v[j+1]);
            }

        }
    }

    emit sendArray(v);
}



// -----------------------------------------------------------------------------------------
// -----------------------------------------------------------------------------------------


Task3::Task3(QObject *parent) : QObject(parent)
{

}


void Task3::InsertSort(QVector<int>& a,  int n)
{
    for (int i = 0; i < n - 1; ++i)
    {
        int end = i;
        // 单趟排序：[0, end]有序 end+1位置的值，插入进入，保持他依旧有序
        int tmp = a[end + 1];
        while (end >= 0)
        {
            if (tmp < a[end])
            {
                a[end + 1] = a[end];
                --end;
            }
            else
            {
                break;
            }
        }
        a[end + 1] = tmp;
    }
}


void Task3::working(QVector<int> v)
{
    qDebug() << QThread::currentThreadId() ;
    InsertSort(v, v.size());
    emit sendArray(v);
}


